package icom.acm.util.mysql;

import java.sql.Connection;
import java.sql.SQLException;

import icom.acm.util.spring.SpringFactory;

import org.apache.tomcat.dbcp.dbcp.BasicDataSource;


public class MysqlDB {
	private static MysqlDB mysql;
	private BasicDataSource source;
	/**
	 * @param classname	驱动类名
	 * @param url		连接
	 * @param username	用户名
	 * @param password	密码
	 * @param initialsize	初始化连接大小
	 * @param maxActive		最大连接数量
	 * @param minIdle		最小空闲连接数
	 * @param maxIdle		最大空闲连接数
	 * @param maxWait		超时等待时间以毫秒为单位
	 * @param logAbandoned	是否在自动收回超时连接的时候打印连接的超时错误
	 * @param removeAbandoned 是否自动回收超时连接
	 * @param removeAbandonedTimeout 超时时间
	 */
	public MysqlDB(String classname,
			String url,String username, String password,
			Integer initialsize, Integer maxActive,
			Integer minIdle, Integer maxIdle, Integer maxWait,
			Boolean logAbandoned, Boolean removeAbandoned, 
			Integer removeAbandonedTimeout){
		source = new BasicDataSource();
		source.setDriverClassName(classname);
		source.setUrl(url);
		source.setUsername(username);
		source.setPassword(password);
		source.setInitialSize(initialsize);
		source.setMaxActive(maxActive);
		source.setMinIdle(minIdle);
		source.setMaxIdle(maxIdle);
		source.setMaxWait(maxWait);
		source.setLogAbandoned(logAbandoned);
		source.setRemoveAbandoned(removeAbandoned);
		source.setRemoveAbandonedTimeout(removeAbandonedTimeout);		
	}
	
	public synchronized static MysqlDB newInstance(){
		if (mysql==null) {
			SpringFactory sf = SpringFactory.newInstance();
			mysql = (MysqlDB)sf.action(sf.bean, "MysqlDB");
		}
		return mysql;
	}
	
	public Connection getConnection() throws SQLException{
		return source.getConnection();
	}
}
